<template>
<div class="edit">
  <!--搜索-->
  <div class="search">
    <span>关键词搜索查询：</span>
    <el-input @clear="searchDataInfo(1)" @keyup.enter.native="searchDataInfo(1)" class="searchInput" placeholder="请输入查询内容" v-model="search.searchVal" clearable>
    </el-input>
    <el-button class="searchBtn" type="primary" @click.native="searchDataInfo(1)" icon="el-icon-search">搜索
    </el-button>
    <el-button class="searchBtn" type="warning" @click.native="clearDate" icon="el-icon-delete">重置
    </el-button>
  </div>
  <!--标签-->
  <div class="label">
    <span>标签查询：</span>
    <el-tag v-for="(val,index) in tag" :key="index" class="labelItem" :class="{active:tag[index].br}" @click.native="searchTab(index,val,tag)">
      {{val.text}}
    </el-tag>
    <el-button @click="moreTag" class="more" size="mini" type="primary">
      更多&nbsp;&nbsp;<i :class="{'el-icon-arrow-right': moreshow,'el-icon-arrow-down':!moreshow}"></i></el-button>
  </div>
  <!--结果-->
  <el-table class="table" ref="muTable" @row-click="checkRow" :data="infoDate">
    <el-table-column type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="问题：" v-for="(v,i) in props.row.s_qstarr" :key="i">
            <span>{{ v.qustion }}</span>
          </el-form-item>
          <el-form-item label="答案：">
            <span>{{ props.row.s_answer }}</span>
          </el-form-item>
          <el-form-item label="标签：">
            <el-tag style="margin-left: 5px" v-for="tt in props.row.s_tags" :key="tt" :type=" !tt ? 'info' : ''">
              {{tt?tt:'无标签'}}
            </el-tag>
          </el-form-item>
          <el-form-item label="关键词：">
            <el-tag type="success" style="margin-left: 5px" v-for="tt in props.row.s_keywords.split(',')" :key="tt">
              {{tt}}
            </el-tag>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
    <el-table-column label="问题">
      <template slot-scope="scope">
        {{scope.row.s_qstarr[0].qustion.substring(0,10)+'...'}}
      </template>
    </el-table-column>
    <el-table-column label="答案" :formatter="answer" prop="s_answer">
    </el-table-column>
    <el-table-column label="标签" prop="s_tags">
      <template slot-scope="scope">
        <el-tag v-for="t in scope.row.s_tags.slice(0,2)" :key="t" style="margin: 4px 10px" :type=" !t ? 'info' : ''">
          {{t ? t : '无标签'}}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column label="关键字" prop="s_keywords">
      <template slot-scope="scope">
        <el-tag v-for="t in scope.row.s_keywords.split(',').slice(0,2)" :key="t" style="margin: 4px 10px" type="success">
          {{t}}
        </el-tag>
      </template>
    </el-table-column>

    <el-table-column label="操作" width="260">
      <template slot-scope="scope">
        <el-button size="mini" @click="goLink(scope.row._id)">编辑</el-button>
        <el-button size="mini" type="success" @click="goLink(scope.row._id,false)">添加子问题</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--分页-->
  <div class="page">
    <el-pagination background layout="total, prev, pager, next, jumper"
                   @current-change="handleCurrentChange"
                   :current-page="page.currentPage"
                   :page-size="page.pagesize"
                   :total="len">
    </el-pagination>
  </div>
</div>

</template>
<script>
import { searchData, getTag } from '../api/getData'

export default {
  name: 'info',
  data: function () {
    return {
      search: {
        searchVal: '',
        searchTag: [],
        searchSubmitStr: '' // 需要提交数据存一下转换格式
      },
      page: {
        currentPage: 1, // 当前页
        pagesize: 10 // 每页显示数量
      },
      tag: [],
      moretag: [],
      moreshow: true,
      infoDate: [], // 请求的数据
      len: 0 // 总数据
    }
  },
  created: function () {
    this.getTags()
  }, // 创建时
  beforeMount: function () {}, // 挂载之前
  mounted: function () {
    this.searchDataInfo(1)
  }, // 挂载之后
  beforeUpdate: function () {}, // 数据更新时调用,在渲染之前
  updated: function () {}, // 数据更新后,渲染后调用(禁止)
  beforeDestroy: function () {}, // 实例销毁前调用,解绑中间层的数据传输
  destroyed: function () {}, // 实例销毁后调用
  watch: {
    '$route': 'searchDataInfo(1)'
  },
  methods: {
    clearDate () {
      this.search.searchSubmitStr = ''
      this.search.searchVal = ''
      this.getTags()
      this.search.searchTag = []
      this.searchDataInfo(1)
    },
    checkRow (row, event, column) {
      if (column.label === '操作') {
        return false
      }
      this.$refs.muTable.toggleRowExpansion(row)
    },
    getTags () {
      getTag().then(({data}) => {
        let arr = []
        for (let i of data) {
          arr.push({
            text: i,
            br: false
          })
        }
        this.moretag = arr
        this.tag = arr.splice(0, 9)
      }).catch(err => {
        this.$notify({
          title: '获取标签失败',
          position: 'top-right',
          message: err,
          type: 'error'
        })
      })
    },
    /* 标签高亮 */
    searchTab (index, val, arr) {
      console.log(arr)
      arr[index].br = !arr[index].br
      if (arr[index].br) {
        this.search.searchTag.push(arr[index].text)
      } else {
        let newVal = val.text
        this.search.searchTag.splice(this.search.searchTag.indexOf(newVal), 1)
      }
      // 请求数据
      this.searchDataInfo(1)
    },
    /* 更多标签 */
    moreTag () {
      this.moreshow = !this.moreshow
      if (this.moreshow) {
        this.tag = this.tag.splice(0, 9)
      } else {
        this.tag = [...this.tag, ...this.moretag]
      }
    },
    /* searchMoreTab (index, val) {
      this.moretag[index].br = !this.moretag[index].br
      if (this.moretag[index].br) {
        this.search.searchTag.push(this.moretag[index].text)
      } else {
        var newVal = val.text
        this.search.searchTag.splice(this.search.searchTag.indexOf(newVal), 1)
      }
      // 请求数据
      this.searchDataInfo()
    }, */
    searchDataInfo (page = this.page.currentPage) { // 请求数据
      // 转换数组
      this.search.searchSubmitStr = this.search.searchTag.join(' ').toString()
      searchData({
        tag: this.search.searchSubmitStr,
        keywords: this.search.searchVal,
        pagenum: String(page)
      }).then(res => {
        console.log(res.data)
        this.infoDate = Array.isArray(res.data[0]) ? res.data[0] : []
        this.len = res.data[1]
        if (page === 1) {
          this.page.currentPage = 1
        }
      }).catch(err => {
        console.log('--err--', err)
      })
    },
    goLink (id, flag = true) {
      let url = flag ? `/amend/${id}` : `/add/${id}`
      this.$router.push(url)
    },
    /* 跳转 */
    handleCurrentChange: function (currentPage) {
      console.log('--currentPage--', currentPage)
      this.page.currentPage = currentPage
      this.searchDataInfo(currentPage)
    },
    /* 过滤table标签数据 */
    fitertags (data) {
      return data.s_tags.length > 1 ? data.s_tags[0] + ',' + data.s_tags[1] + '...' : data.s_tags
    },
    /* 过滤table答案 */
    answer (data) {
      return data.s_answer.length > 10 ? data.s_answer.substr(0, 10) + '...' : data.s_answer
    },
    /* 过滤table答案 */
    keywords (data) {
      return data.s_answer.length > 15 ? data.s_keywords.substr(0, 10) + '...' : data.s_keywords
    }
  } // 函数
}
</script>
<style lang='scss'>
.edit {
  font-family: "微软雅黑";
  box-sizing: border-box;
  width: 100%;
  .search {
    width: 80%;
    margin-left: 10%;
    text-align: left;

    .searchInput {
      width: 50%;
    }
    .searchBtn {
      margin-left: 2%;
    }
  }
  .label {
    width: 80%;
    margin-left: 10%;
    margin-top: 20px;
    text-align: left;
    position: relative;
    .labelItem {
      font-size: 12px;
      margin-left: 10px;
      margin-bottom: 10px;
      color: #409EFF;
      background-color: #fff;
      user-select: none;
      cursor: pointer;
      &.active {
        color: #fff;
        background-color: #409EFF;
      }
    }
    .more {
      position: absolute;
      right: -80px;
      top: 2px;
    }
  }
  .moretag {
    width: 80%;
    margin-left: 10%;
    margin-top: 0;
    margin-bottom: 20px;
    text-align: center;
    .titletag {
      color: #99A9BF;

    }
  }
  .table {
    width: 100%;
    padding: 20px;
    .demo-table-expand {
      font-size: 0;
    }
    .demo-table-expand label {
      display: block;
      width: 90px;
      color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
      margin-right: 0;
      margin-bottom: 0;
      width: 100%;
      overflow: hidden;
    }
  }
  .page {
    margin-top: 20px;
  }
  /*修改*/
  .el-main {
    line-height: 1px !important;
  }
  .table .demo-table-expand label {
    float: left;
  }
  .el-form--inline .el-form-item__content {
    text-align: left;
    float: left;
    width: 85%;
  }
  .el-table th {
    text-align: center;
  }
  .el-collapse-item__header {
    height: 34px;
    line-height: 34px;
  }
  .el-collapse-item__arrow {
    line-height: 34px;
  }
}
</style>
